package com.juku2024.juku.fragment

import android.content.Intent
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.juku2024.juku.*
import com.uu.main.callback.OnNoDoubleItemClickListener
import com.juku2024.juku.adapter.MeAdapter
import com.juku2024.juku.base.BaseFragment
import com.juku2024.juku.db.NoteRoomUtils
import com.juku2024.juku.model.MeListModel
import com.juku2024.juku.tools.AppUtils
import com.juku2024.juku.tools.WrapLinearLayoutManager

class MeFragment: BaseFragment() {

    private val meAdapter by lazy { MeAdapter(MeListModel.getMeList()) }

    override fun layoutView(inflater: LayoutInflater, container: ViewGroup?): View {
        return inflater.inflate(R.layout.fragment_me, container, false)
    }

    override fun initView(rootView: View) {
        super.initView(rootView)
        val meRecycler: RecyclerView = rootView.findViewById(R.id.meRecycler)
        meRecycler.layoutManager = WrapLinearLayoutManager(requireContext())
        meRecycler.setHasFixedSize(true)
        meRecycler.adapter = meAdapter
    }


    override fun bindEvent() {
        super.bindEvent()
        meAdapter.setOnItemClickListener(object: OnNoDoubleItemClickListener(){
            override fun onNoDoubleClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                val intent = when(position){
                    0 -> Intent(requireContext(), HistoryActivity::class.java)
                    1 -> Intent(requireContext(), FavoriteActivity::class.java)
                    2 -> Intent(requireContext(), DownloadDetailActivity::class.java)
                    3 -> Intent(requireContext(), FeedbackActivity::class.java)
                    4 -> {
                        //获取剪贴版
                        val dao = NoteRoomUtils.getInstance().homeTabDao()
                        var value = dao.query()?.shareUrl
                        if (TextUtils.isEmpty(value)) {
                            value = AppUtils.getShareUrl(requireContext())
                        }
                        val shareIntent = Intent()
                        shareIntent.action = Intent.ACTION_SEND
                        shareIntent.type = "text/plain"
                        //要分享的文本内容，选择某项后会直接把这段文本发送出去，相当于调用选中的应用的接口，并传参
                        shareIntent.putExtra(Intent.EXTRA_TEXT, value)
                        //需要使用Intent.createChooser，这里我们直接复用。第二个参数并不会显示出来
                        Intent.createChooser(shareIntent, value)
                    }
                    5 -> { // 常见问题
                        Intent(requireContext(), CommonProblemActivity::class.java)
                    }
                    else -> Intent(requireContext(), SettingActivity::class.java)
                }
                startActivity(intent)
            }
        })
    }

}